﻿package Cain
{
	import flash.display.DisplayObject;
	import flash.geom.Point;
	
	/**
	 * 計算角度
	 * @author Cain@2009 in ADCAST
	 */
	public class roMc 
	{
		static private var mc:DisplayObject;
		static private var po:Point;
		static private var mc_po:Point;
		/**
		 * 取得角度
		 * @param	_mc 中心物件
		 * @param	_po 目標點
		 * @return 旋轉的夾角
		 */
		static public function ro(_mc:DisplayObject,_po:Point=null):Number 
		{
			mc = _mc;
			mc_po = new Point(mc.x, mc.y);
			mc_po = mc.parent.localToGlobal(mc_po);
			if (_po==null) 
			{
				po = new Point(mc.stage.mouseX,mc.stage.mouseY);
			}else {
				po = _po;
			}
			return getDtoP(po,mc_po).Radian;
		}
		/**
		 * 計算夾角(目標點與滑鼠)
		 * @param	Target 
		 * @param	Center
		 * @return 旋轉的夾角
		 */
		static public function getDtoP(Target:Point, Center:Point = null):Object
		{
			var obj:Object = new Object();
			obj.Length = Point.distance(Center, Target);
			obj.Radian = Math.acos((Target.x-Center.x)/obj.Length)*180/Math.PI;
			if(Target.y<Center.y){
				obj.Radian = 360-obj.Radian
			}
			return obj
		}
	}	
}